#include <iostream>
#include <SimpleAmqpClient/SimpleAmqpClient.h> //MQ CPP库
#include <nlohmann/json.hpp>                   // json库
#include "OssManage.h"

using namespace std;
using namespace AmqpClient;
using nlohmann::json;

int main()
{
    // 定义MQ连接URI
    string uri = "amqp://guest:guest@localhost:5672/%2f";
    Channel::ptr_t channel = Channel::CreateFromUri(uri);

    // 2指定要连接的队列名称
    const string &q = "queue1";

    // 启动消费者，告诉MQ，准备从队列q 消费信息
    channel->BasicConsume(q);

    // 创建 oss 对象，用来执行文件上传
    OssManage *oss = OssManage::get_instance();

    // 循环接收消息
    for (;;)
    {
        // 阻塞式获取消息
        Envelope::ptr_t envelope = channel->BasicConsumeMessage();

        // 收到消息，进行反序列化
        if (envelope && envelope->Message())
        {
            // 将消息体反序列化为JSON对象
            nlohmann::json message = nlohmann::json::parse(envelope->Message()->Body());
            string object = message["object"];
            string file = message["file"];

#ifdef DEBUG
            cout << "object: " << object << ", file: " << file << endl;
#endif
            // 调用OSS上传接口，上传文件
            if (!oss->upload_object("xia-cpp", object, file))
            {
                cerr << "upload object failed" << object
                     << ", file: " << file << endl;
            }
        }
    }
    OssManage::destroy_instance();
}